<template>
  <a-modal
    v-model:visible="props.addShowblePop"
    @cancel="emits('update:addShowblePop', false)"
    :footer="null"
    width="60%"
    title="新增子题"
  >
    <a-form :model="Data.addCheringneData" @finish="handeledAddcheng" :rules="Data.addChenRules">
      <a-row :gutter="24" class="p-5">
        <a-col :span="8">
          <a-form-item label="温馨提示">子题分数和需和父题分数相同 </a-form-item>
        </a-col>
        <a-col :span="16">
          <a-form-item label="题目分值"> {{ isBatchHasObj.tmfz }}</a-form-item>
        </a-col>
        <a-col :span="8" v-if="!isBatchHasObj?.isjdFale">
          <a-form-item label="题目编号" name="tmbh">
            <a-input placeholder="题目编号" v-model:value="Data.addCheringneData.tmbh" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="题目满分" name="tmfz">
            <a-input-number
              placeholder="题目满分"
              :width="'100%'"
              step="0.5"
              min="0.5"
              v-model:value="Data.addCheringneData.tmfz"
            />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item :wrapper-col="{ offset: 12, span: 16 }">
            <a-button type="primary" html-type="submit" :loading="Data.loading">保存</a-button>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-modal>
</template>

<script lang="ts" setup>
import { reactive, watch } from 'vue'
// 消息提示框
import { useMessage } from '/@/hooks/web/useMessage'
const { createMessage } = useMessage()
import { pyzzOperateSave } from '/@/api/examinationAudit'
const emits = defineEmits(['update:addShowblePop', 'success'])

const Data = reactive({
  addCheringneData: {},
  addChenRules: {
    tmbh: [{ required: true, message: '请输入试题编号' }],
    tmfz: [{ required: true, message: '请输入试题分值' }],
  },
  loading: false,
} as any)

const props = defineProps({
  addShowblePop: {
    type: Boolean,
    default: false,
  },
  isBatchHasObj: {
    type: Object,
    default: () => ({}),
  },
  kmdm: {
    type: String,
    default: '',
  },
})

watch(
  () => props.addShowblePop,
  () => (Data.addCheringneData = {})
)

async function handeledAddcheng(value: any) {
  let tmxh = props?.isBatchHasObj?.children?.length ? '' : 1
  if (props?.isBatchHasObj?.children?.length) {
    props?.isBatchHasObj?.children.sort((a, b) => a.tmxh - b.tmxh)
    tmxh = props?.isBatchHasObj?.children[props.isBatchHasObj.children.length - 1].tmxh + 1
  }
  let { code, message } = await pyzzOperateSave({
    kmdm: props.kmdm,
    tmlx: props.isBatchHasObj.tmlx,
    tmxh,
    tmbh: Data.addCheringneData.tmbh,
    tmfz: Data.addCheringneData.tmfz,
    sjtmid: props.isBatchHasObj.id,
  })
  if (code === 200) {
    createMessage.info('新增成功')
    emits('success')
  } else {
    createMessage.error(message)
  }
}
</script>
